import { cloneDeep } from 'lodash-es'
import { Ref, UnwrapRef, ref } from 'vue'

/**
 * @param initValue 初始化的值
 * @returns [响应式的ref, 初始化函数]
 */
export default function useProRef<T>(
  initValue: T
): [Ref<UnwrapRef<T>>, (resetValue?: { [index: string]: any }) => void] {
  const _ref = ref<T>(initValue)
  const _initValue = cloneDeep(initValue) as any

  function reset(resetValue?: { [index: string]: any }) {
    if (Object.prototype.toString.call(initValue) === '[object Object]') {
      _ref.value = cloneDeep(Object.assign({}, _initValue, resetValue)) as any
    } else {
      _ref.value = _initValue
    }
  }

  return [_ref, reset]
}
